<?php
header("Content-Type: text/html; charset=utf-8");
require_once '../setting/setting.php';
require_once '../smarty/Smarty.class.php';
require_once '../lib/query_railway.php';
require_once '../lib/base.php';
$user=$_SESSION["RU"];
//var_dump($user);
if(!$user)header("Location:../user/login.php");
if($user["nick"]!="admin")exit;
if($_POST["act"]=="get_ss") {
	$code=$_POST["c"];
	$str=" code='".$code."'";
	$ss=QR::check_exist($tb_end,$str);
	if($ss){
		echo $ss["en"];
	}else{
		echo "-1";	
	}
	exit;
}
if($_POST["act"]=="modify") {
	//i:i,s:s,e:e,z:z,g2:g2,g4:g4,h4:h4,r:r
	$i=urldecode($_POST["i"]);
	
	$end_station=urldecode($_POST["s"]);
	$border_station=urldecode($_POST["z"]);
	$box_type=urldecode($_POST["b"]);
	
	$g2=urldecode($_POST["g2"]);
	$g4=urldecode($_POST["g4"]);
	$h4=urldecode($_POST["h4"]);
	
	$vtime=urldecode($_POST["t"]);
	$remark=urldecode($_POST["r"]);
	
	$check_station=QR::check_exist($tb_end,"en='".$end_station."'");
	if(!$check_station){echo "站名有误，请重新输入！";exit;}
	
	$dbtable =$tb_border_end;
	if($i){
		$_set="set p20=$g2,p40gp=$g4,p40hc=$h4,box='".$box_type."',stime='".$vtime."',ctime=NOW(),remark='".$remark."' where id=".$i;
		$op=QR::update_data($dbtable,$_set);
		if($op>0)echo "更新成功！" ;else echo "更新失败，请检查输入的正确性！";
	}else{
		$_sql="end='".$end_station."' and border='".$border_station."' and box='".$box_type."'";
		$update=QR::check_exist($dbtable,$_sql);
		if(!$update){
			$data[0]="start,border,end,p20,p40gp,p40hc,box,stime,ctime,remark";
			$data[1]="'$end_station','$border_station','$end_station',$g2,$g4,$h4,'$box_type','$vtime',NOW(),'$remark'";		
			$op= QR::add_data($dbtable,$data);
			if($op>0)echo "添加成功！" ;else echo "添加失败，请检查输入的正确性！";
		}else{
			echo "同样条件的数据已存在！添加数据失败！";
		}
	}
	exit;
}

$rid=$_GET["i"];
if($rid){
	$str=" id='".$rid."'";
	$data=QR::get_data($tb_border_end, "*",$str,0,1);
	//var_dump($data);
}

$end=QR::get_field($tb_end,"en");
$tran=QR::get_field($tb_border,"cn");
$box_type=QR::get_field($tb_box,"type");
//echo $start;
$smarty = new Smarty;
$smarty->assign("leftpanel",leftpanel("RW",6));
$smarty->assign("szdata",$data[0]);
$smarty->assign("rid",$rid);
$smarty->assign("end",json_encode($end));
$smarty->assign("tran",$tran);
$smarty->assign("box",$box_type);
$smarty->assign("user",$user);
$smarty->display('railway_modify_ze.tpl');

?>